import {
    equipmentList,
    equipmentDetalie,
    equipmentModify
} from '../../api/pay'
import user from '../../../utils/user'
import {toast,confirm} from '../../../utils/util'
Page({
    data: {
        list:[],
        listZans:[],
        pageLock:false,
        pageNum:1,
        pageSize:1000,
        popupShow:false,
        id:'',
        isSelect:false,
        selectedIdList:[],
        isAdmin: true,
        isSuperAdmin: false
    },
    onLoad(options){
        console.log(options,'options')
        this.setData({
            isSelect:options.isSelect,
            isSuperAdmin: user.isSuperAdmin
        })
        
    },
    onShow: function () {
        user.waitLogin().then(()=>{
            this.onEquipmentList()
        })
    },
    onToAdd(){
        wx.navigateTo({
            url:'/packagePay/pages/device-add/device-add'
        })
    },
    onEquipmentList(){
        console.log(this.data.pageNum)
        equipmentList({
            pageSize:this.data.pageSize,
            pageNum:this.data.pageNum
        }).then(res=>{
            if (!res.hasNextPage) {
                this.setData({
                    pageLock:true
                })
            }
            this.setData({
                list:res.list
            })
            this.data.list.forEach((item,index)=>{
                this.data.list[index].selected = false
            })
            console.log(this.data.list,'this.data.list')
            const eventChannel = this.getOpenerEventChannel()
            eventChannel.on('acceptDataFromOpenerPage',(list)=>{
                console.log(list,'data')
                this.data.list.forEach((item,index)=>{
                    if(list.data){
                        list.data.forEach((ite)=>{
                            if(item.id==ite){
                                this.setData({
                                    ['list[' + index + '].selected'] : true
                                })
                            }
                        })
                    }
                })
                this.data.selectedIdList.push(...list.data)
            })
        })
    },
    scroll(){
        if (this.data.pageLock) {
            return
        }
        this.setData({
            pageNum: this.data.pageNum += 1
        },()=>{
            console.log(this.data.pageNum)
        })
        this.onEquipmentList()
    },
    showActionSheet(event){
        const { id } = event.currentTarget.dataset
        this.setData({
            id:id
        })
        const t = this
        wx.showActionSheet({
            itemList: ['编辑', '删除'],
            success (res) {
                console.log(res.tapIndex,'调用成功')
                if(res.tapIndex === 0){
                    t.setData({
                        popupShow:true
                    })
                }else{
                    confirm('使用该设备的项目将会自动变为无需硬件打卡').then(()=>{
                        equipmentDetalie({
                            id:id
                        }).then(()=>{
                            toast('删除成功')
                            t.setData({
                                listZans:[],
                                pageNum:1
                            })
                            t.onEquipmentList()
                        })
                    })
                }
            },

        })
    },
    bindcancel(){
        this.setData({
            popupShow:false
        })
    },
    bindsubmit(res){
        console.log(res,'res')
        const {name} = res.detail
        if(!name){
            return
        }
        equipmentModify({
            id:this.data.id,
            remark:name
        }).then(()=>{
            this.setData({
                listZans:[],
                pageNum:1,
                popupShow:false
            })
            this.onEquipmentList()
            toast('修改成功')
        })

    },
    switchSelect(event){
        const { id } = event.currentTarget.dataset
        let isOF = this.data.selectedIdList.indexOf(id)
        console.log(isOF)
        if(isOF === -1){
            // 添加暂存列表id
            this.data.selectedIdList.push(id)
        }else{
            // 删除暂存列表id
            this.data.selectedIdList.splice(isOF,1)
        }
        // 改变list数组里的select状态
        this.data.list.forEach((item,index)=>{
            if(item.id === id){
                this.setData({
                    // ['book[' + i + '].selected']: !isSelected
                    ['list[' + index +'].selected'] : !this.data.list[index].selected
                })
            }
        })
    },
    onComplete(event){
        const { type } = event.currentTarget.dataset
        if(type){
            wx.navigateBack(-1)
            return
        }
        let arr = []
        console.log(this.data.selectedIdList,this.data.list)
        this.data.list.forEach((item)=>{
            this.data.selectedIdList.forEach(ite=>{
                if(item.id == ite){
                    arr.push(item)
                }
            })
        })
        // console.log(arr,'arr')
        const eventChannel = this.getOpenerEventChannel()
        eventChannel.emit('someEvent', {data: this.data.selectedIdList})
        eventChannel.emit('acceptDataFromOpenedPage', {data: arr,name:'已开启硬件打卡'})
        wx.navigateBack(-1)
    }
})